package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.PermissionService;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Reference
    private RoleService roleService;

    @Reference
    private PermissionService permissionService;

    @Reference
    private MenuService menuService;

    /**
     * 分页查询角色信息
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public Result findByPage(QueryPageBean queryPageBean){
        log.info("[角色管理-分页查询]data:{}",queryPageBean);
        PageResult pageResult = roleService.findPage(queryPageBean);
        return new Result(true, MessageConst.ACTION_SUCCESS,pageResult);
    }

    /**
     * 添加、编辑角色窗口页面权限信息
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
       List<Permission> permissions= permissionService.findAll();
        return new Result(true,MessageConst.ACTION_SUCCESS,permissions);
    }

    /**
     * 添加、编辑角色窗口页面菜单信息
     * @return
     */
    @RequestMapping("/findMenu")
    public Result findMenu(){
        List<Menu> menus = menuService.findMenu();
        log.info("[角色管理-菜单页面显示]menus:{}",menus);
        return new Result(true,MessageConst.ACTION_SUCCESS,menus);
    }
    /**
     * 添加角色
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds){
        log.info("[角色管理-新增角色]role:{},permissionIds:{},menuIds:{}",role,permissionIds,menuIds);
        roleService.add(role,permissionIds,menuIds);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    /**
     * 请求角色信息
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
    log.info("[角色管理-回显角色信息]id:{}",id);
    Role role = roleService.findById(id);
    return new Result(true,MessageConst.ACTION_SUCCESS,role);
    }

    /**
     * 回显角色权限关联ID
     * @param id
     * @return
     */
    @RequestMapping("/findRoleIdByPermissionIds")
    public Result findRoleIdByPermissionIds(Integer id){
        log.info("[角色管理-回显角色权限关联ID]id:{}",id);
        List<Integer> permissionIds =roleService.findRoleIdByPermissionIds(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,permissionIds);
    }

    /**
     * 回显角色菜单关联ID
     * @return
     */
    @RequestMapping("/findRoleIdByMenuIds")
    public Result findRoleIdByMenuIds(Integer id){
        log.info("[角色管理-回显角色菜单关联ID]id:{}",id);
        List<Integer> menuIds = roleService.findRoleIdByMenuIds(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,menuIds);
    }
    /**
     * 编辑角色信息
     * @param role
     * @param permissionIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        log.info("[角色管理-编辑角色信息]role:{},permissionIds:{},menuIds:{}",role,permissionIds,menuIds);
        roleService.edit(role,permissionIds,menuIds);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        roleService.deleteById(id);
        return new Result(true, "删除角色成功");
    }
    @GetMapping("/findAll")
    public Result findAll(){
        //rpc查询
        List<Role> roles = roleService.findAll();
        //返回结果
        return new Result(true, MessageConst.ACTION_SUCCESS,roles);
    }
}
